package com.person;

import com.person.mapper.UserMapper;
import com.person.pojo.User;

import com.person.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.ServerException;


@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServerException, IOException {

        String username=request.getParameter("username");
        String password=request.getParameter("password");


        SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession=sqlSessionFactory.openSession();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user=userMapper.select(username,password);
        sqlSession.close();

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        PrintWriter writer=response.getWriter();

        if(user!=null){
            writer.write("登录成功");
        }else {
            writer.write("登录失败");
        }
    }
    @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServerException, IOException{
        this.doGet(request,response);
    }
}
